package com.chazz.dao.impl;

import com.chazz.dao.UserDao;
import com.chazz.entity.Orders;
import com.chazz.entity.User;
import com.chazz.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.chazz.entity.Adress;

import java.util.ArrayList;
import java.util.List;


public class UserDaoImpl implements UserDao {

    @Override
    public User findUserById(int id) throws SQLException {
        User user=null;
        String sql="SELECT * FROM user WHERE id=?";
        ResultSet rs= DBUtils.executeQuery(sql,id);
        if(rs.next()){
            user=new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setEmail(rs.getString("email"));
            user.setPhone(rs.getString("phone"));
            user.setSex(rs.getInt("sex"));
        }
        rs.close();
        DBUtils.close();
        return user;
    }
    @Override
    public User findUserByEmail(String email) throws SQLException {
        User user=null;
        String sql="SELECT * FROM user WHERE email=?";
        ResultSet rs= DBUtils.executeQuery(sql,email);
        if(rs.next()){
            user=new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setEmail(rs.getString("email"));
            user.setPhone(rs.getString("phone"));
            user.setSex(rs.getInt("sex"));
        }
        rs.close();
        DBUtils.close();
        return user;
    }

    @Override
    public int findUserFinalId() throws SQLException {
        String sql="select * from user order by id DESC limit 1";
        ResultSet rs= DBUtils.executeQuery(sql,null);
        System.out.println(rs);
        User user=null;
        if(rs.next()){
            user=new User();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setEmail(rs.getString("email"));
            user.setPhone(rs.getString("phone"));
            user.setSex(rs.getInt("sex"));

        }
            rs.close();
        DBUtils.close();
        return user.getId();
    }

    public int findOrderFinalId() {
        String sql="select * from orders order by id DESC limit 1";
        ResultSet rs= DBUtils.executeQuery(sql,null);
        System.out.println(rs);
        Orders orders = new Orders();
        try {
            if(rs.next()){
                orders.setId(rs.getInt("id"));
                orders.setUid(rs.getInt("uid"));
                orders.setTatolprice(rs.getString("tatolprice"));
                orders.setState(rs.getInt("state"));
                orders.setCreatetime(rs.getDate("createtime"));
                orders.setOrderadress(rs.getString("orderadress"));
                orders.setIsdelete(rs.getInt("isdelete"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        DBUtils.close();
        return orders.getId()+1;
    }




    @Override
    public int updateByUser(User user) {
        //获取修改后的用户信息
        String userEmail=user.getEmail();
        String userPhone=user.getPhone();
        int sex=user.getSex();
        String userName=user.getUsername();
        String sql="update user set username=?,phone=?,sex=? where email=?";
        Object[] parama={userName,userPhone,sex,userEmail};
        //返回受影响的行数
        int rt=DBUtils.executeUpdate(sql,parama);
        return rt;
    }

    @Override
    public int deletUser(User user) {
        //保留方法
        return 0;
    }


    @Override
    public boolean updateUserPassword(User user) throws SQLException {
        String sql="update user set id=?,username=?,password=?,phone=?,sex=? where email=?";
        Object[] params = new Object[] {
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getPhone(),
                user.getSex(),
                user.getEmail()
        };

        return DBUtils.executeUpdate(sql, params)>0 ;
    }

    @Override
    public boolean addUser(User user) {
        String sql="insert into user(id,username,password,email,phone,sex)VALUES(?,?,?,?,?,?)";
        Object [] objects=new Object[]{
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                user.getEmail(),
                user.getPhone(),
                user.getSex()
        };


        return DBUtils.executeUpdate(sql,objects)>0;
    }



    @Override
    public List<Adress> findAddressByUid(Integer userid) {
        List<Adress> adresses = new ArrayList<>();
        String sql = "SELECT * FROM Adress WHERE uid=?";
        Object[] param = new Object[]{
          userid
        };
        ResultSet rs = DBUtils.executeQuery(sql,param);
        try{
            while(rs.next()){
                Adress adress = new Adress();
                adress.setId(rs.getInt("id"));
                adress.setUid(rs.getInt("uid"));
                adress.setAdressvalue((rs.getString("adressvalue")));
                adresses.add(adress);
            }
        }catch (SQLException ex){
            ex.printStackTrace();
        }
        return adresses;
    }

}
